//
//  ZNetworkTool.m
//  FastMedicine
//
//  Created by zero on 17/3/6.
//  Copyright © 2017年 zero. All rights reserved.
//

#import "ZNetworkTool.h"
#import "ZUserAccountViewModel.h"
#import "ZUserInfoModel.h"

@implementation ZNetworkTool

#pragma mark
#pragma mark - 单例
+ (instancetype)shareNetworkTool{
    
    static ZNetworkTool *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [self manager];
        
        //  可接受格式
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain", nil];
        
        //  请求格式（默认：二进制）
        instance.requestSerializer = [AFJSONRequestSerializer serializer];
    });
    
    return instance;
}

#pragma mark
#pragma mark - get方法
- (void)GETWithUrlString:(NSString *)urlString parameters:(id)parameters success:(void(^)(id responseObject))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    [self GET:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        // 把AFN获取的responseObject,回调给外界
        if (successBlock) {
            successBlock(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}

#pragma mark
#pragma mark - post方法
- (void)POSTWithUrlString:(NSString *)urlString parameters:(id)parameters success:(void(^)(id responseObject))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    [self POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        // 把AFN获取的responseObject,回调给外界
        if (successBlock) {
            successBlock(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failureBlock) {
            failureBlock(error);
        }
    }];
}

#pragma mark
#pragma mark - homeLoad
- (void)homeLoadSuccess:(void(^)(id responseObject))successBlock failure:(void(^)(NSError *error))failureBlock{
    
    
    NSString *urlString = @"https://api.weibo.com/2/statuses/home_timeline.json";
    
    ZUserInfoModel *model = [ZUserAccountViewModel shareUserAccountViewModel].model;
    
    NSDictionary *para = @{@"access_token" : model.access_token
                           };
    
    [self GETWithUrlString:urlString parameters:para success:^(id responseObject) {
        
        if (successBlock) {
            successBlock(responseObject);
        }
        
        
    } failure:^(NSError *error) {
        
        
        if (failureBlock) {
            
            failureBlock(error);
        }
    }];
}
@end
